// pages/xinzengshdz/xinzengshdz.js
import { areaList} from './data';
const {addaddress,getxq,removeadd,updataaddress}=require('../../http/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    areaList:areaList,
    show: false,
    adress:'',
    linkMan:'',
    mobile:'',
    dress:'',
    state:true,
    id:'',
  },
  remove(){
      removeadd(wx.getStorageSync('token'),this.data.id).then(res=>{
        wx.redirectTo({
          url: '/pages/shouhuodizhi/shouhuodizhi',
        })
      })
  },
  add(){
    if(this.data.adress.length<=1){
      wx.showToast({
        title: '请选择省份',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(this.data.linkMan==''){
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(this.data.mobile==''){
      wx.showToast({
        title: '请填写手机号码',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if(this.data.dress==''){
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 1000
      })
      return
    }
    let obj={
      linkMan:this.data.linkMan,
      mobile:this.data.mobile,
      address:this.data.dress,
      cityId:this.data.adress[1].code,
      provinceId:this.data.adress[0].code,
      districtId:this.data.adress[2].code
    }
    if(this.data.state){
      addaddress(wx.getStorageSync('token'),obj).then(res=>{
       
      })
    }else{
      updataaddress(wx.getStorageSync('token'),obj,this.data.id).then(res=>{
        console.log(res)
      })
    }
    wx.redirectTo({
      url: '/pages/shouhuodizhi/shouhuodizhi',
    })
  },
  huoqu(){
    let that=this
    wx.chooseAddress({
      success(res){
        console.log(res)
        let obj=res
        let arr=[
          {code:440000 ,name:obj.provinceName},
          {code:440100 ,name:obj.cityName},
          {code:440105 ,name:obj.countyName},
        ]
        that.setData({
          adress:arr,
          linkMan:obj.userName,
          mobile:obj.telNumber,
          dress:obj.detailInfo,
        })
      }
    })
  },
  qr(arr){
    this.onClose() 
    console.log(arr.detail.values)
    this.setData({
      adress:arr.detail.values
    })
  },
  qx(){
    this.onClose() 
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  addname(e){
    this.setData({
        linkMan:e.detail
    })
 
  },
  addmoble(e){
    this.setData({
      mobile:e.detail
  })
 
  }, adddress(e){
    this.setData({
      dress:e.detail
  })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.id==''){
      console.log(111)
    }else{
      getxq(wx.getStorageSync('token'),options.id).then(res=>{
        console.log(res.data.data.info)
        let obj=res.data.data.info
        let arr=[
          {code:obj.provinceId,name:obj.provinceStr},
          {code:obj.cityId,name:obj.areaStr},
          {code:obj.districtId,name:obj.cityStr},
        ]
        this.setData({
          adress:arr,
          state:false,
          linkMan:obj.linkMan,
          mobile:obj.mobile,
          dress:obj.address,
          id:obj.id,
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})